"use strict";
//监听器
class Emitter1 {
    constructor() {
        this.events = new Map();
    }
    once(event, cb) {
        // 实现触发一次订阅的方法
        //创建一个自定义函数，通过on触发，触发后立马通知off()
        const callback = (...args) => {
            cb(...args);
            this.off(event, callback);
        };
        this.on(event, callback);
    }
    on(event, cb) {
        // 实现订阅方法
        if (this.events.has(event)) {
            //map中有事件
            const cbList = this.events.get(event);
            cbList && cbList.push(cb);
        }
        else {
            this.events.set(event, [cb]);
        }
    }
    off(event, cb) {
        // 实现删除订阅方法
        const cbList = this.events.get(event);
        if (cbList) {
            //有事件
            const list = cbList.filter(fn => fn !== cb);
            if (list.length > 0) {
                this.events.set(event, list);
            }
            else {
                this.events.delete(event);
            }
        }
        else {
            this.events.delete(event);
        }
    }
    emit(event, ...args) {
        // 实现派发方法
        const cbList = this.events.get(event);
        console.log(cbList);
        cbList && cbList.forEach(fn => {
            fn(...args);
        });
    }
}
const bus1 = new Emitter1();
// bus.on('msg',()=>{
//   console.log('点击了');
// })
const cb1 = (...args) => {
    console.log(...args, '222');
};
//bus.on('msg',cb)
//bus.emit('msg','你好')
//bus.off('msg',cb)
bus1.once('message', cb1);
bus1.emit('message', 'hello');
console.log(bus1);
